package org.example.shop;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class ManagerMenu {

        HashMap<String, Goods> goodsMap = new HashMap<>();         //用于存放商品信息
        Scanner sc = new Scanner(System.in);
        public void managerMenu(ManagerMenu me, Manager m, HashMap<String, Customer> customerMap, HashMap<String, Customer> IDMap) {
            boolean flag = true;
            do {
                System.out.println("管理员登录，请输入账号和密码");
                String accountNum = sc.next();
                String code = sc.next();
                me.ManagerSignIn(accountNum, code, m);//管理员登录
                if (accountNum.equals(m.getAccountNum()) && code.equals(m.getCode())) {
                    flag = false;
                }
            } while (flag);//登录完毕
            //菜单界面
            boolean flag2 = true;
            do {
                System.out.println("菜单：1.修改管理员自身密码 2.修改客户密码 3.删除客户信息 4.查询客户信息 ");
                System.out.println("     5.添加商品信息 6.修改商品信息 7.删除商品信息 8.查询商品信息 9.退出");
                int choose = sc.nextInt();
                if (choose == 1) {                            //修改管理员自身密码
                    System.out.println("请输入新密码");
                    String code = sc.next();
                    m.changeMyCode(code);
                    System.out.println("修改成功");
                } else if (choose == 2) {                      //修改客户密码
                    m.changeCustomerCode(customerMap);
                } else if (choose == 3) {                     //删除客户信息
                    deleteCustomerInformation(customerMap, IDMap);
                } else if (choose == 4) {
                    System.out.println("请输入查询方式：1.ID查询 2.用户名查询 3.全部查询");
                    int select = sc.nextInt();
                    if (select == 1) {
                        IDSearch(IDMap);
                    } else if (select == 2) {
                        nameSearch(customerMap);
                    } else if (select == 3) {
                        allSearch(customerMap);
                    } else {
                        System.out.println("输入错误");
                    }
                } else if (choose == 5) {
                    addGoods();                          //添加商品信息
                } else if (choose == 6) {
                    changeGoods();                       //修改商品信息
                } else if (choose == 7) {
                    delGoods();                          //删除商品信息
                } else if (choose == 8) {
                    searchGoods();                       //查询商品信息
                } else if (choose == 9) {                //退出
                    System.out.println("退出");
                    flag2 = false;
                } else {
                    System.out.println("输入错误");
                }
            } while (flag2);
        }

        public void ManagerSignIn(String accountNum, String code, Manager m) {
            if (accountNum.equals(m.getAccountNum()) && code.equals(m.getCode())) {
                System.out.println("登录成功");
            } else {
                System.out.println("登录失败");
            }
        }

        public void deleteCustomerInformation(HashMap<String, Customer> customerMap, HashMap<String, Customer> IDMap) {
            System.out.println("请输入要删除的客户名和ID");
            String name = sc.next();
            String ID = sc.next();
            if (customerMap.get(name) == null || IDMap.get(ID) == null) {
                System.out.println("用户不存在");
            } else {
                System.out.println("请确认是否删除，1.删除  2.取消");
                int choose = sc.nextInt();
                if (choose == 1) {
                    customerMap.remove(name);
                    IDMap.remove(ID);
                    System.out.println("删除成功");
                }
            }
        }

        public void IDSearch(HashMap<String, Customer> IDMap) {
            System.out.println("请输入ID");
            String ID = sc.next();
            if (IDMap.get(ID) == null) {
                System.out.println("用户不存在");
            } else {
                Customer c4 = IDMap.get(ID);
                System.out.println("客户信息：ID 用户名 用户级别 注册时间 客户累计消费总金额 用户手机号 用户邮箱");
                System.out.println("      " + c4.getCustomerID() + " " + c4.getCustomerName() + " " + c4.getCustomerGrade() +
                        "  " + c4.getRegisterTime() + "     " + c4.getConsumptionTotal() + "    "
                        + c4.getCustomerPhoneNum() + "  " + c4.getCustomerEmail());
            }
        } public void nameSearch(HashMap<String, Customer> customerMap) {
            System.out.println("请输入用户名");
            String name = sc.next();
            if (customerMap.get(name) == null) {
                System.out.println("用户名不存在");
            } else {
                Customer c4 = customerMap.get(name);
                System.out.println("客户信息：ID 用户名 用户级别 注册时间 客户累计消费总金额 用户手机号 用户邮箱");
                System.out.println("      " + c4.getCustomerID() + " " + c4.getCustomerName() + " " + c4.getCustomerGrade() +
                        "  " + c4.getRegisterTime() + "     " + c4.getConsumptionTotal() + "    "
                        + c4.getCustomerPhoneNum() + "  " + c4.getCustomerEmail());
            }
        }

        public void allSearch(HashMap<String, Customer> customerMap) {
            System.out.println("客户信息：ID  用户名 用户级别 注册时间 客户累计消费总金额 用户手机号 用户邮箱");
            Set<String> keys = customerMap.keySet();
            for (String key : keys) {
                Customer value = customerMap.get(key);
                System.out.println("      " + value.getCustomerID() + " " + value.getCustomerName() + " " + value.getCustomerGrade()
                        + "  " + value.getRegisterTime() + "     " + value.getConsumptionTotal() + "    "
                        + value.getCustomerPhoneNum() + " " +value.getCustomerEmail());
            }
        }

        public void addGoods() {
            System.out.println("请输入商品的信息：商品编号、商品名称、生产厂家、生产日期、型号、进货价、零售价格、数量");
            String goodsNum = sc.next();
            String goodsName = sc.next();
            String factory = sc.next();
            String date = sc.next();
            String num = sc.next();
            double buyPrice = sc.nextDouble();
            double retailPrice = sc.nextDouble();
            int total = sc.nextInt();
            Goods g = new Goods(goodsNum, goodsName, factory, date, num, buyPrice, retailPrice, total);//添加商品信息至对象
            goodsMap.put(goodsName, g);
            System.out.println("添加成功");
        }

        public void changeGoods() {
            System.out.println("请输入商品名称");
            String goodsName = sc.next();
            if (goodsMap.get(goodsName) == null) {
                System.out.println("该商品不存在");
            } else {
                System.out.println("请输入商品的新信息：商品编号、商品名称、生产厂家、生产日期、型号、进货价、零售价格、数量");
                String goodsNum = sc.next();   goodsName = sc.next();
                String factory = sc.next();
                String date = sc.next();
                String num = sc.next();
                double buyPrice = sc.nextDouble();
                double retailPrice = sc.nextDouble();
                int total = sc.nextInt();
                Goods g2 = new Goods(goodsNum, goodsName, factory, date, num, buyPrice, retailPrice, total);//添加商品信息至对象
                goodsMap.put(goodsName, g2);          //保存
            }
        }

        public void delGoods() {
            System.out.println("请输入商品名称");
            String goodsName = sc.next();
            if (goodsMap.get(goodsName) == null) {
                System.out.println("该商品不存在");
            } else {
                System.out.println("删除后无法恢复，请选择：1.删除 2.取消");
                int select = sc.nextInt();
                if (select == 1) {
                    goodsMap.remove(goodsName);
                    System.out.println("删除成功");
                }
            }
        }

        public void searchGoods() {System.out.println("请输入商品名称");
            String goodsName = sc.next();
            if (goodsMap.get(goodsName) == null) {
                System.out.println("该商品不存在");
            } else {
                Goods g = goodsMap.get(goodsName);
                System.out.println("商品信息：商品编号、商品名称、生产厂家、生产日期、型号、进货价、零售价格、数量");
                System.out.println("          " + g.getGoodsNum() + "    " + g.getGoodsName() + "     " + g.getFactory() + "  "
                        + g.getDate() + "    " + g.getNum() + "  " + g.getBuyPrice() + "  " + g.getRetailPrice() + "   " + g.getTotal());
            }
        }
    }
